﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using System;
	using System.Web.Mvc;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Core.Validators;

	public class UserCreate : ValueBase<UserCreate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserCreate"/> class.
		/// </summary>
		public UserCreate()
		{
			// services
			this.CountryService = Application.Resolve<ICountryService>();
			this.TimeZoneService = Application.Resolve<ITimeZoneService>();

			// default values
			this.CountryId = Settings.Countries.Default;
			this.TimeZoneId = Settings.TimeZones.Default;
			this.Dst = UserPreferenceDaylightSavingTime.Auto;
			this.StartOfWeek = UserPreferenceStartOfWeek.Monday;
			this.TimeFormat = UserPreferenceTimeFormat.Relative;
		}

		/// <summary>
		/// Gets or sets the country repository.
		/// </summary>
		/// <value>The country repository.</value>
		public ICountryService CountryService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the time zone repository.
		/// </summary>
		/// <value>The time zone repository.</value>
		public ITimeZoneService TimeZoneService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the country id.
		/// </summary>
		/// <value>The country id.</value>
		public int CountryId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the time zone id.
		/// </summary>
		/// <value>The time zone id.</value>
		public int TimeZoneId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the email address.
		/// </summary>
		/// <value>The email address.</value>
		public string EmailAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the verify email address.
		/// </summary>
		/// <value>The verify email address.</value>
		public string VerifyEmailAddress
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the password.
		/// </summary>
		/// <value>The password.</value>
		public string Password
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the verify password.
		/// </summary>
		/// <value>The verify password.</value>
		public string VerifyPassword
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the status.
		/// </summary>
		/// <value>The status.</value>
		public UserStatus Status
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the DST.
		/// </summary>
		/// <value>The DST.</value>
		public UserPreferenceDaylightSavingTime Dst
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the start of week.
		/// </summary>
		/// <value>The start of week.</value>
		public UserPreferenceStartOfWeek StartOfWeek
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the time format.
		/// </summary>
		/// <value>The time format.</value>
		public UserPreferenceTimeFormat TimeFormat
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the countries.
		/// </summary>
		/// <value>The countries.</value>
		public SelectList Countries
		{
			get
			{
				return new SelectList(this.CountryService.GetAll(), "Id", "Title", this.CountryId);
			}
		}

		/// <summary>
		/// Gets the time zones.
		/// </summary>
		/// <value>The time zones.</value>
		public SelectList TimeZones
		{
			get
			{
				return new SelectList(this.TimeZoneService.GetAll(), "Id", "Display", this.TimeZoneId);
			}
		}

		/// <summary>
		/// Gets the statuses.
		/// </summary>
		/// <value>The statuses.</value>
		public SelectList Statuses
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserStatus)), this.Status.Name());
			}
		}

		/// <summary>
		/// Gets the DSTS.
		/// </summary>
		/// <value>The DSTS.</value>
		public SelectList Dsts
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserPreferenceDaylightSavingTime)), this.Dst.Name());
			}
		}

		/// <summary>
		/// Gets the start of weeks.
		/// </summary>
		/// <value>The start of weeks.</value>
		public SelectList StartOfWeeks
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserPreferenceStartOfWeek)), this.StartOfWeek.Name());
			}
		}

		/// <summary>
		/// Gets the time formats.
		/// </summary>
		/// <value>The time formats.</value>
		public SelectList TimeFormats
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserPreferenceTimeFormat)), this.TimeFormat.Name());
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserCreate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Name)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Name));

			rules.Add(x => x.Name)
				.Custom(new UserUniqueNameValidator(), Resources.Errors.UserNameTaken);

			rules.Add(x => x.EmailAddress)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.EmailAddress));

			rules.Add(x => x.EmailAddress)
				.Custom(new UserUniqueEmailAddressValidator(), Resources.Errors.UserEmailAddressTaken);

			rules.Add(x => x.VerifyEmailAddress)
				.Equal(x => x.EmailAddress, Resources.Errors.Equal.Fmt(Resources.Titles.EmailAddress, Resources.Titles.VerifyEmailAddress));

			rules.Add(x => x.Password)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Password));

			rules.Add(x => x.VerifyPassword)
				.Equal(x => x.Password, Resources.Errors.Equal.Fmt(Resources.Titles.Password, Resources.Titles.VerifyPassword));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}